import std.concurrency, std.stdio, std.exception, std.variant;
import std.c.time;


import Definitions.msgCatalogue;
import Definitions.procNames;
import Registry.registry;

// In mongoose.c
extern (C) bool mg_hl_start();
extern (C) void mg_hl_stop();

// Main processing loop for the http server
//===========================================================================================
Tid init_http() {
    // A http thread
    // Serves static content

    auto httptid = spawn(&httpproc);

    return httptid;
}

void httpproc() {

    // Create the class that will read rx blocks from the device.
    writeln("Creating http server");
    auto ohttp = new HttpServer();
    ohttp.process();
}

//===========================================================================================
class HttpServer {

    this() {
    }

    void process() {

        bool running = true;
        while (running) {
            writeln("Http server starting");
            if (mg_hl_start() != false) {
                writeln("Http server running");
            }
            else {
                writeln("Http server failed to start");
                break;
            }

            receive(
                // Wait for termination
                (M_TERM discriminator) {
                    mg_hl_stop();
                    running = false;
                }
            );
        }
    }
}
